﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DlutOneKey.Web
{
    public class AutoSelect
    {

        public static async Task StartAutoSelect(Operations operations, List<string> classIds, CancellationToken? token = null)
        {
            var ct = token ?? CancellationToken.None;
            foreach (var classId in classIds)
            {
                if (ct.IsCancellationRequested)
                    break;

                operations.SelectClassByClassId(classId);
                string result = operations.GetSelectResult();
                Console.WriteLine($"{classId} {result}。");

                if (result.Contains("选课成功"))
                {
                    Console.WriteLine($"{classId}选课成功。停止该门课的自动选课。");
                    break;
                }

                operations.ClickCloseButton();

                try
                {
                    await Task.Delay(1000, ct);
                }
                catch (TaskCanceledException)
                {
                    break;
                }
            }
        }

    }
}
